package com.teamtgv.shareshark;

import java.util.ArrayList;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;

import com.teamtgv.shareshark.R;
import com.teamtgv.shareshark.location.LocationRetriever;
import com.teamtgv.shareshark.result.Result;
import com.teamtgv.shareshark.task.AskRecommendationsTask;

public class AskActivity extends Activity {

	public static final String LOG_TAG = AskActivity.class.getSimpleName();
	
	private ProgressDialog pDialog;
	private EditText what;
	private EditText where;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		//Remove title bar
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_ask);
		
		what = (EditText) AskActivity.this.findViewById(R.id.textAskWhat);
		where = (EditText) AskActivity.this.findViewById(R.id.textAskWhere);
		
		// Show result list on share click
		Button askButton = (Button) this.findViewById(R.id.buttonDoAsk);
		askButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (! allFieldsEntered()) {
					showFieldsAlert();
					return;
				}
				
				pDialog = new ProgressDialog(AskActivity.this);
			    pDialog.setMessage("Loading data...");
		        pDialog.setIndeterminate(false);
		        pDialog.setCancelable(false);
		        pDialog.show();
		        
				Intent myIntent = new Intent(AskActivity.this,ResultListActivity.class);
				AskRecommendationsTask askTast = new AskRecommendationsTask(AskActivity.this);
				
				askTast.execute(what.getText().toString(), where.getText().toString());
				try {
					ArrayList<Result> results = askTast.get();
					if (results == null) {
						Toaster.showToast(AskActivity.this, "There was a problem retrieving the results.");
						return;
					}
					myIntent.putParcelableArrayListExtra(ResultListActivity.RESULT_LIST, results);
					AskActivity.this.startActivity(myIntent);
				} catch (Exception e) {
					e.printStackTrace();
					Log.v(LOG_TAG, e.toString() + ": " + e.getMessage());
				}
				pDialog.dismiss();
			}
		});
		
		final Button location = (Button) this.findViewById(R.id.buttonAskGPS);
		location.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				EditText location = ((EditText) AskActivity.this.findViewById(R.id.textAskWhere));
				LocationRetriever locationRetriever = new LocationRetriever(AskActivity.this);
				location.setText(locationRetriever.getLocation());
			}
		});
	}

	private boolean isEmptyOrBlank(String str) {
		return (str == null || str.trim().length() == 0);
	}

	private boolean allFieldsEntered() {
		EditText what = (EditText) AskActivity.this.findViewById(R.id.textAskWhat);
		EditText where = (EditText) AskActivity.this.findViewById(R.id.textAskWhere);
		if (isEmptyOrBlank(what.getText().toString()) || isEmptyOrBlank(where.getText().toString())) {
			return false;
		}
		return true;
	}
	
	private void showFieldsAlert() {
		Toaster.showToast(AskActivity.this, "Please enter all fields");
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		//getMenuInflater().inflate(R.menu.ask, menu);
		return false;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			// This ID represents the Home or Up button. In the case of this
			// activity, the Up button is shown. Use NavUtils to allow users
			// to navigate up one level in the application structure. For
			// more details, see the Navigation pattern on Android Design:
			//
			// http://developer.android.com/design/patterns/navigation.html#up-vs-back
			//
			NavUtils.navigateUpFromSameTask(this);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

}
